// ml278
// Copyright (C) 2008 Bjørn Øivind Bjørnsen, Aleksander Gjermundsen, Knut Andre Tidemann, Thomas Hansen
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#include "planetaryscenemanager.h"
#include "planetaryoptions.h"
#include "planetaryheightmapdata.h"
#include "planetaryplanet.h"
#include "planetaryindexbuffermanager.h"

#include "extendedcamera.h"

namespace Ogre
{
	//-----------------------------------------------------------------------
	// FACTORY
	//-----------------------------------------------------------------------
	const String PlanetarySceneManagerFactory::FACTORY_TYPE_NAME = "PlanetarySceneManager";

	void PlanetarySceneManagerFactory::initMetaData(void) const
	{
		mMetaData.typeName = FACTORY_TYPE_NAME;
		mMetaData.description = "Scenemanager optimised for drawing planets with paging.";
		mMetaData.sceneTypeMask = ST_EXTERIOR_REAL_FAR; // support all types
		mMetaData.worldGeometrySupported = true;
	}

	SceneManager* PlanetarySceneManagerFactory::createInstance(
		const String& instanceName)
	{
		return new PlanetarySceneManager(instanceName);
	}

	void PlanetarySceneManagerFactory::destroyInstance(SceneManager* instance)
	{
		delete instance;
	}
	//-----------------------------------------------------------------------
	// SCENE MANAGER
	//-----------------------------------------------------------------------
	PlanetarySceneManager::PlanetarySceneManager(const Ogre::String &name) : OctreeSceneManager(name)
	{
		mOptions = new PlanetaryOptions(this);
		mHeightmapData = new PlanetaryHeightmapData(this);
		mIndexBufferMgr = new PlanetaryIndexBufferManager(this);

		mPlanet = NULL;

		mWorldGeometryIsSetup = false;
		
		mBulletPhysicsEnabled = false;
	}

	PlanetarySceneManager::~PlanetarySceneManager()
	{
		clearScene();
	}

	void PlanetarySceneManager::setWorldGeometry(const String &filename)
	{
		if (filename.empty())
		{
			DataStreamPtr myStream;

			myStream.setNull();
			setWorldGeometry (myStream);
			return;
		}		

		// try to open in the current folder first
		std::ifstream fs;
		fs.open(filename.c_str(), std::ios::in | std::ios::binary);
		if (fs)
		{
			// Wrap as a stream
			DataStreamPtr myStream (new FileStreamDataStream (filename, &fs, false));
			setWorldGeometry (myStream);
			return;
		}

		// otherwise try resource system
		DataStreamPtr myStream (ResourceGroupManager::getSingleton().openResource(filename));
		setWorldGeometry(myStream);
	}

	void PlanetarySceneManager::setWorldGeometry(DataStreamPtr &stream, const String &typeName)
	{
		mOptions->load(stream);

		assert(mPlanet == NULL);

		mPlanet = new PlanetaryPlanet(this);
		mPlanet->init();
		mPlanet->load();


		mWorldGeometryIsSetup = true;
	}

	Camera * Ogre::PlanetarySceneManager::createCamera(const String & name)
	{
		Camera * c = new ExtendedCamera( name, this );
		mCameras.insert( CameraList::value_type( name, c ) );

		// create visible bounds aab map entry
		mCamVisibleObjectsMap[c] = VisibleObjectsBoundsInfo();
	
		return c;
	}
	
	std::vector< PlanetaryBody * > Ogre::PlanetarySceneManager::getPlanetaryBodies()
	{
		std::vector<PlanetaryBody*> bodies;
		bodies.push_back((PlanetaryBody*)mPlanet);
		return(bodies);
	}

	void PlanetarySceneManager::clearScene()
	{
		if(mWorldGeometryIsSetup)
		{
			mPlanet->unload();
			mPlanet->uninit();
			delete mPlanet;
			mPlanet = NULL;

			mWorldGeometryIsSetup = false;
		}

		OctreeSceneManager::clearScene();
	}
}

